package fun.sanguo.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import fun.sanguo.model.dto.*;
import fun.sanguo.model.dto.interact.InteractPersonageLikeInsert;
import fun.sanguo.model.dto.interact.InteractPersonageLikeResult;
import fun.sanguo.model.dto.interact.InteractPersonageLikeUpdate;
import fun.sanguo.model.enums.ResEnums;
import fun.sanguo.service.interact.InteractPersonageLikeService;
import fun.sanguo.service.sys.SysUserService;
import fun.sanguo.utils.HttpUtils;
import fun.sanguo.utils.exception.ArtificialException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 互动-人物喜欢记录 服务接口
 *
 * @author ZYP
 * @date 2023-10-13
 */
@RestController
public class InteractPersonageLikeController {

    protected Logger logger = LoggerFactory.getLogger(getClass());
    protected Logger sys_user_logger = LoggerFactory.getLogger("sys-user");

    @Resource
    InteractPersonageLikeService interactPersonageLikeService;

    @Resource
    SysUserService sysUserService;

    /**
     * 页面新增
     *
     * @param reqDto
     * @param request
     * @return
     */
    @RequestMapping(value = "/interact/personage/like/insert")
    @ResponseBody
    public ResDto insert(@RequestBody ReqDto reqDto, HttpServletRequest request) {
        // 定义响应内容
        ResDto resDto = new ResDto();
        try {
            // 请求日志
            sys_user_logger.info("接受来自IP={}的新增人物喜欢记录请求, {}", HttpUtils.getIpAddr(request), reqDto.toString());
            // 获取请求包体
            PageInsert<InteractPersonageLikeInsert> interactPersonageLikeInsertPageInsert = (PageInsert<InteractPersonageLikeInsert>) reqDto.getData();
            // 业务处理，未登陆将抛异常
            DataInfo<InteractPersonageLikeResult> interactPersonageLikeResultDataInfo = interactPersonageLikeService.insert(interactPersonageLikeInsertPageInsert, sysUserService.getCurrentUserId(), HttpUtils.getIpAddr(request));
            // 封装响应
            resDto.setCode(ResEnums.SUCCESS.getCode());
            resDto.setMsg(ResEnums.SUCCESS.getMsg());
            resDto.setData(interactPersonageLikeResultDataInfo);
            // 响应日志
            sys_user_logger.info(resDto.toString());
        } catch (ArtificialException ae) {
            logger.error("新增人物喜欢记录过程中发生异常，exception={}", ae.getMsg());
            // 封装响应
            resDto.setCode(ae.getCode());
            resDto.setMsg(ae.getMsg());
        } catch (Exception e) {
            logger.error("新增人物喜欢记录过程中发生异常，exception={}", e.getMessage());
            // 封装响应
            resDto.setCode(ResEnums.EXCEPTION.getCode());
            resDto.setMsg(ResEnums.EXCEPTION.getMsg() + ": " + e.getMessage());
        }
        return resDto;
    }

    /**
     * 页面删除
     *
     * @param reqDto
     * @param request
     * @return
     */
    @RequestMapping(value = "/interact/personage/like/delete")
    @ResponseBody
    public ResDto delete(@RequestBody ReqDto reqDto, HttpServletRequest request) {
        // 定义响应内容
        ResDto resDto = new ResDto();
        try {
            // 请求日志
            sys_user_logger.info("接受来自IP={}的删除人物喜欢记录请求, {}", HttpUtils.getIpAddr(request), reqDto.toString());
            // 获取请求包体
            PageUpdate<InteractPersonageLikeUpdate> interactPersonageLikeUpdatePageUpdate = (PageUpdate<InteractPersonageLikeUpdate>) reqDto.getData();
            // 业务处理，未登陆将抛异常
            DataInfo<InteractPersonageLikeResult> interactPersonageLikeResultDataInfo = interactPersonageLikeService.delete(interactPersonageLikeUpdatePageUpdate, sysUserService.getCurrentUserId());
            // 封装响应
            resDto.setCode(ResEnums.SUCCESS.getCode());
            resDto.setMsg(ResEnums.SUCCESS.getMsg());
            resDto.setData(interactPersonageLikeResultDataInfo);
            // 响应日志
            sys_user_logger.info(resDto.toString());
        } catch (ArtificialException ae) {
            logger.error("删除人物喜欢记录过程中发生异常，exception={}", ae.getMsg());
            // 封装响应
            resDto.setCode(ae.getCode());
            resDto.setMsg(ae.getMsg());
        } catch (Exception e) {
            logger.error("删除人物喜欢记录过程中发生异常，exception={}", e.getMessage());
            // 封装响应
            resDto.setCode(ResEnums.EXCEPTION.getCode());
            resDto.setMsg(ResEnums.EXCEPTION.getMsg() + ": " + e.getMessage());
        }
        return resDto;
    }
}
